#include <REGX52.H>
#include "LCD1602.h"
#include "DELAY.h"
#include "MatrixKey.h"

unsigned char KeyNum;
unsigned int Password,Count;

void main()
{
  LCD_Init();
	while(1)
	{
		LCD_ShowString(1,1,"Password");
		KeyNum=MatrixKey();
		if(KeyNum)													//不加判断会一直显示00，只有按下的一瞬间会变成keynum
		{
			if(Count<4)												//判断密码是否超过四位
			{
					if(KeyNum<=10)
					{
							Password*=10;     				//密码左移
							Password+=KeyNum%10;			//获取一位密码
					}
					Count++;											//计次加一	
			}
			LCD_ShowNum(2,1,Password,4);			//显示密码
		}
		if(KeyNum==11) 											//如果S11按下，确认
		{
			if(Password==1234)								//如果密码等于正确密码
			{
				LCD_ShowString(1,14,"OK ");			//显示OK
				Password=0;
				Count=0;
				LCD_ShowNum(2,1,Password,4); 
			}
			else															//如果密码不等于正确密码
			{
				LCD_ShowString(1,14,"ERR");			//显示ERR
				Password=0;
				Count=0;
				LCD_ShowNum(2,1,Password,4);
			}			
		}
		if(KeyNum==12)
		{
			Password=0;
			Count=0;
			LCD_ShowNum(2,1,Password,4);
		}
	}
}
